package boggle;

import java.util.*;
import java.io.*;
import java.util.zip.*;

public class WordList implements Iterable<String> {
	
	private HashSet<String> dict = new HashSet<String>();  // storing word lists
	
	
	public WordList() {	}
	
	public WordList(File file) throws IOException {
		
		BufferedReader br = new BufferedReader(new FileReader(file));
		while (true) {
			String line = br.readLine();  // one word, one line
			if (line == null) break;
			add(line);
		}
		br.close();
	}
	
	public WordList(InputStream is) throws IOException {
		InputStreamReader isr = new InputStreamReader(is);
		BufferedReader br = new BufferedReader(isr);
		while (true) {
			String line = br.readLine();  // one word, one line
			if (line == null) break;
			add(line);
		}
		br.close();
	}
	
	public static WordList loadFromZipFile(File zipFile) throws IOException {
		FileInputStream fis = new FileInputStream(zipFile);
		ZipInputStream  zis  = new ZipInputStream(fis);
		ZipEntry entry;
		entry = zis.getNextEntry();  // must do this !
		//System.out.println("Extracting: " + entry);
		WordList wlist = new WordList(zis);
		return wlist;
	}
	
	
	/**
	 * Return the size of word list
	 */
	public int size() { return dict.size(); }
	
	
	/**
	 * add word to wordlist
	 * @param word word to add to dict
	 */
	public void add(String word) {
		String w = preprocessing(word);
		if (w.length() != 0)
			dict.add(word.toLowerCase().trim());
	}
	
	/*
	 * remove the leading and trail blanks of word
	 * and change word into lowercase
	 */
	private String preprocessing(String word) {
		return word.toLowerCase().trim();
	}
	
	public boolean containsWord(String word) {
		String w = preprocessing(word);
		return dict.contains(w);
	}
	
	/**
	 * add word in wordlist b to a
	 * @param b wordList to add to a
	 */
	public void addWordList(WordList b) {
		for(String word : b.dict) 
			dict.add(word);
	}
	
	public void subtract(WordList b) {
		for(String word : b.dict) {
			if (this.containsWord(word)) 
				this.dict.remove(word);
		}
	}
	
	
	@Override
	public Iterator<String> iterator() {
		return dict.iterator();
	}
		
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		if (args.length == 1) {
			File file = new File(args[0]);
			WordList wlist;
		//	try {
		//		wlist = new WordList(file);
		//		System.out.println("wlist.containsWord(aberrant):" + wlist.containsWord("aberrant"));
		//	} catch (IOException e) {
		//		System.out.println("Failed to create wordlist!!");
		//		e.printStackTrace();
		//	}
			try {
				wlist = WordList.loadFromZipFile(file);
				System.out.println("wlist.containsWord(ablaze):" + wlist.containsWord("ablaze"));
			//	for (String word : wlist.dict) 
			//		System.out.println(word);
				
				
			} catch (IOException e) {
				System.out.println("Failed to create wordlist!!");
				e.printStackTrace();
			}
			
		}
		else {
			System.out.println("Illegal argument!");
		}

	}

	

}
